package com.gton.util;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.baomidou.mybatisplus.generator.fill.Property;
import com.baomidou.mybatisplus.generator.keywords.MySqlKeyWordsHandler;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @description: Mybatis-Plus3.5.1代码生成器
 * @author: GuoTong
 * @createTime: 2022-02-13 16:59
 * @since JDK 1.8 OR 11
 **/
public class AutoGeneratorTools {

    private final String url = "jdbc:mysql://127.0.0.1:3306/study?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC";

    // 高版本的DataSource不需要驱动 保留
    private final String driver = "com.mysql.jdbc.Driver";

    private final String username = "root";
    private final String password = "guotong199114";

    /**
     * Description:
     * 使用前：
     * 指定生成的模块名称：modelName
     * 指定生成业务子包名称：mkdirFiile
     *
     * @author: GuoTong
     * @date: 2022-10-22 22:03:01
     */
    private static String modelName = "/usermanager/";
    // 按照业务划分不同多个子包
    private static String mkdirFiile = "usermanage";

    public static void mainFuction() {
        AutoGeneratorTools autoGeneratorTools = new AutoGeneratorTools();
        String userDir = System.getProperty("user.dir");
        String path = "src/main/java";
        String parentPath = "com.gton." + mkdirFiile;
        String mapperXmlPath = "/com/gton/" + mkdirFiile + "/mapper/xml";
        List<String> tables = new ArrayList<>();
        tables.add("gateway_rutor");

        FastAutoGenerator.create(autoGeneratorTools.getDataConfigBuilder()).
                globalConfig(builder ->
                        builder.author("GuoTong").
                                commentDate("yyyy-MM-dd").
                                enableSwagger().
                                dateType(DateType.TIME_PACK).
                                disableOpenDir().
                                outputDir(userDir + modelName + path)).
                packageConfig(builder ->
                        builder.parent(parentPath).
                                moduleName("").
                                entity("entity").
                                service("service").
                                serviceImpl("service.impl").
                                mapper("mapper").
                                xml("mapper.xml").
                                controller("controller").
                                pathInfo(Collections.singletonMap(OutputFile.mapperXml, userDir + modelName + path + mapperXmlPath))).
                strategyConfig(builder ->
                        builder.addInclude(tables).
                                entityBuilder().
                                idType(IdType.ASSIGN_ID).
                                enableLombok().
                                enableTableFieldAnnotation().
                                enableChainModel().
                                logicDeleteColumnName("is_del").
                                logicDeletePropertyName("isDel").
                                addTableFills(new Column("create_time", FieldFill.INSERT)).
                                addTableFills(new Property("createTime", FieldFill.INSERT)).
                                addTableFills(new Column("update_time", FieldFill.INSERT_UPDATE)).
                                addTableFills(new Property("updateTime", FieldFill.INSERT_UPDATE)).
                                controllerBuilder().
                                formatFileName("%sController").
                                enableRestStyle().
                                serviceBuilder().
                                formatServiceFileName("%sService").
                                formatServiceImplFileName("%sServiceImpl").
                                mapperBuilder().
                                enableBaseColumnList().
                                enableMapperAnnotation().
                                enableBaseResultMap().
                                formatMapperFileName("%sMapper").
                                formatXmlFileName("%sMapper")).
                templateEngine(new VelocityTemplateEngine()).execute();

    }

    private DataSourceConfig.Builder getDataConfigBuilder() {

        return new DataSourceConfig.Builder(url, username, password).
                dbQuery(new MySqlQuery()).
                keyWordsHandler(new MySqlKeyWordsHandler()).
                schema("Mybatis-Plus").
                typeConvert(new MySqlTypeConvert());
    }
}
